package jpb.util.load;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by hegc on 2016/7/20.
 * Excel解析类
 */
public final class FileUpLoadUtil {

        private static Logger logger = LoggerFactory.getLogger(FileUpLoadUtil.class);

        /**
         * 上传文件服务类
         *
         * @param file      原始上传文件
         * @param uploadDir 文件存放目录
         * @return
         */
        public static void upload(MultipartFile file, String uploadDir) {
                File desc = null;
                String rename = file.getOriginalFilename();
                File dir = new File(uploadDir);
                try {
                        if (!dir.exists()) {
                                dir.mkdirs();
                                logger.info("文件存放目录{}不存在，开始新建目录。", uploadDir);
                        }
                        desc = new File(dir, rename);
                        file.transferTo(desc);
                } catch (IOException e) {
                        logger.error("上传文件出错，错误详情：{}", e.getMessage());

                }
                logger.info("上传文件成功。上传文件路径为：{}", desc.getAbsolutePath());
        }

}
